package tt.com.datamanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.ColdRoom;
import tt.com.common.model.CremateStove;
import tt.com.common.util.PageUtil;
import tt.com.datamanager.mapper.CremateStoveMappers;
import tt.com.datamanager.service.CremateStoveService;

import java.util.Date;

@Service
public class CremateStoveServiceImpl extends ServiceImpl<CremateStoveMappers,CremateStove> implements CremateStoveService {

    @Autowired
    private CremateStoveMappers cremateStoveMappers;

    @Override
    public PageUtil selectAllCremateStoveByPage(PageUtil pageUtil) {
        Page<CremateStove> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<CremateStove> cremateStovePage = baseMapper.selectPage(page, null);
        // 设置总条数 自动计算总页数
        pageUtil.setTotalSize((int) cremateStovePage.getTotal());
        pageUtil.setData(cremateStovePage);
        return pageUtil;
    }

    @Override
    public Integer updateCremateStove(CremateStove cremateStove, String updateUserId) {
        // 设置修改时间
        cremateStove.setLastUpdateTime(new Date());
        // 设置修改操作人id
        cremateStove.setLastUpdateUserId(updateUserId);
        return baseMapper.updateById(cremateStove);
    }

    @Override
    public Integer deleteCremateStove(Long id, String deleteUserId) {
        //根据id获取火化炉
        CremateStove cremateStove = baseMapper.selectById(id);
        if (cremateStove == null){
            //若数据不存在，返回-1
            return -1;
        }
        if (cremateStove.getDataStatus() == 1){
            //判断是否已经被删除过
            return -2;
        }
        //设置删除时间
        cremateStove.setDeleteTime(new Date());
        //设置删除操作人id
        cremateStove.setDeleteUserId(deleteUserId);
        cremateStove.setDataStatus(1);
        baseMapper.updateById(cremateStove);
        return baseMapper.deleteById(cremateStove.getId());
    }

    @Override
    public Integer insertCremateStove(CremateStove cremateStove) {
        QueryWrapper<CremateStove> wrapper = new QueryWrapper<>();
        wrapper.eq("stove_num", cremateStove.getStoveNum());
        if (baseMapper.selectOne(wrapper) != null) {
            // 重复添加返回-1
            return -1;
        }
        // 设置创建时间
        cremateStove.setCreateTime(new Date());
        cremateStove.setDataStatus(0);
        return baseMapper.insert(cremateStove);
    }





}
